//
//  MyViewController.m
//  B项目
//
//  Created by lanou on 16/8/5.
//  Copyright © 2016年 Work. All rights reserved.
//

#import "MyViewController.h"
#import "PrefixHeader.pch"
#import "collectTableViewController.h"
#import "collectMusicTableViewController.h"
//#import "MMDrawerController.h"
//#import "UIViewController+MMDrawerController.h"
#import "LoaginViewController.h"
#import "ThemeManage.h"
#import "UIView+ThemeChange.h"
#import "RegistViewController.h"
#import "MusicTableViewController.h"
#import <AVOSCloud/AVOSCloud.h>
@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *MytableView;
@property(nonatomic,strong)UIView *Myview;
@property(nonatomic,strong) UIImageView *imgView ;

//当前用户
@property(nonatomic,strong)AVUser *currentUser;

@property(nonatomic,strong)NSString *strTitle;

@end


@implementation MyViewController


- (void)viewWillAppear:(BOOL)animated {
    if ([AVUser currentUser] != nil) {
        self.currentUser = [AVUser currentUser];
        NSLog(@"%@", self.currentUser);
        AVFile *file = self.currentUser[@"imageFile"];

         NSLog(@"======%@", file.url);
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:file.url] placeholderImage:[UIImage imageNamed:@"comment_profile_default@2x.png"]];
        [self.loaginBT setTitle:self.currentUser.username forState:UIControlStateNormal];
        
    }


}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";
    self.view.backgroundColor = [UIColor grayColor];
    UIBarButtonItem *rightBT = [[UIBarButtonItem alloc]initWithTitle:@"导航" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem = rightBT;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];

    [self creatMytableView];
    [self creatHeadView];


}
//音乐点击事件
- (void)rightAction
{
    NSLog(@"%@", @"地图导航跳转");
    // 地图导航跳转
//    MusicTableViewController *musicVC = [[MusicTableViewController alloc]init];
//    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:musicVC];
//    [self.navigationController presentViewController:naVC animated:YES completion:nil];
}



- (void)viewWillDisappear:(BOOL)animated
{
    self.imgView.layer.cornerRadius = self.imgView.bounds.size.width /2;
    self.imgView.clipsToBounds = YES;
}
//创建MytableView
- (void)creatMytableView
{
    self.MytableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
    self.MytableView.delegate = self;
    self.MytableView.dataSource = self;
     self.MytableView.bounces = NO;
    self.MytableView.backgroundColor = [UIColor whiteColor];
    _MytableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:_MytableView];
    [self.MytableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}

//创建头视图
- (void)creatHeadView
{
    self.Myview = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth , 180)];
    self.Myview.backgroundColor = [UIColor redColor];
    self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth-80)/2, 40, 80, 80)];
    _imgView.image = [UIImage imageNamed:@"comment_profile_default@2x.png"];
    self.loaginBT = [[UIButton alloc]initWithFrame:CGRectMake((kScreenWidth-120)/2, _imgView.endY + 20, 120, 30)];
    _loaginBT.userInteractionEnabled = YES;

    //自动登陆
    self.currentUser = [AVUser currentUser];

    if (_currentUser != nil) {
        // 跳转到首页
        _strTitle = [AVUser currentUser].username;
        [_loaginBT setTitle:_strTitle forState:UIControlStateNormal];
        self.imgView.image = [UIImage imageWithContentsOfFile:self.imgString];

    } else {
        //缓存用户对象为空时，可打开用户注册界面…
        _strTitle = @"登陆";
        [_loaginBT setTitle:_strTitle forState:(UIControlStateNormal)];

    }

    _loaginBT.titleLabel.textAlignment = NSTextAlignmentCenter;
    _loaginBT.titleLabel.font = [UIFont systemFontOfSize:14];
    [_loaginBT addTarget:self action:@selector(loaginAction) forControlEvents:(UIControlEventTouchUpInside)];
     _MytableView .tableHeaderView = _Myview;
     [_Myview addSubview:_imgView];
    [_Myview addSubview:_loaginBT];


}



//row的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


//section
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
   sectionView.backgroundColor = [UIColor grayColor];
    UILabel *sectionTitle = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 60, 20)];
    sectionTitle.text = @"收藏";
    sectionTitle.font = [UIFont systemFontOfSize:14];
    [sectionView addSubview:sectionTitle];
    return sectionView;
}

//高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}



//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
         return kScreenWidth/10 + 45;
    }else{
        return 40;
    }

}

//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [_MytableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];

    NSArray *imgArray = @[@"NewsCollectede.png",@"VedioCollected.png",@"musicCollected.png"];
    NSArray *titleArray = @[@"新闻",@"视频",@"音乐"];
    NSArray *cellArray = @[@"清除缓存",@"夜间模式",@"仅wifi播放",@"版本号",@"免费声明",@"注销"];
    if (indexPath.row == 0) {
        for (NSInteger i = 0; i < 3; i++) {
            UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
            button.tag = 10+ i;
            button.frame = CGRectMake((kScreenWidth/10)*1.5 + (kScreenWidth/10)*3 *i , 10, (kScreenWidth/10), (kScreenWidth/10));
            [button setImage:[UIImage imageNamed:imgArray[i]] forState:(UIControlStateNormal)];
            [button addTarget:self action:@selector(btClick:) forControlEvents:(UIControlEventTouchUpInside)];

            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth/10)*1.5 + (kScreenWidth/10)*3*i, button.endY +5, (kScreenWidth/10), 20)];
            lable.text = titleArray[i];
            lable.textAlignment =  NSTextAlignmentCenter;
            lable.font = [UIFont systemFontOfSize:14];
            [cell addSubview:button];
            [cell addSubview:lable];

        }

    }else{

        cell.textLabel.text = cellArray[indexPath.row - 1];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        if (indexPath.row == 2) {
        UISwitch *swith1 = [[UISwitch alloc]initWithFrame:CGRectMake(kScreenWidth - 70, 5, 60, 30)];
          [swith1 setOn:NO];

         [swith1 addTarget:self action:@selector(switch1IsChanged:) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:swith1];

        }else if (indexPath.row == 3){
        UISwitch *swith2 = [[UISwitch alloc]initWithFrame:CGRectMake(kScreenWidth - 70, 5, 60, 30)];
        [swith2 setOn:YES];
        [swith2 addTarget:self action:@selector(switch2IsChanged:) forControlEvents:UIControlEventValueChanged];
        [cell addSubview:swith2];

        }else if (indexPath.row == 4){
            UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth -50, 10, 40, 20)];
            lable.text =  @"1.0.0";
            lable.alpha = 0.7;
            lable.font = [UIFont systemFontOfSize:13];
            [cell  addSubview:lable];
        }

    }


    [cell.contentView NightWithType:0];

    [cell NightWithType:0];
    return cell;
}



//登陆点击事件
- (void)loaginAction
{

    if ( _currentUser != nil) {
        _loaginBT.userInteractionEnabled = NO;
    }else{
        LoaginViewController *loaginVC = [[LoaginViewController alloc]init];
        _loaginBT.userInteractionEnabled = YES;
        loaginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loaginVC animated:YES];
        loaginVC.hidesBottomBarWhenPushed = NO;
    }

}

//button点击事件

- (void)btClick:(UIButton *)button
{
    NSInteger index = button.tag;
    if (index == 10) {
        collectTableViewController *collectTVC = [[collectTableViewController alloc]init];
        collectTVC.navigationItem.title = @"新闻收藏";
        [self.navigationController pushViewController:collectTVC animated:YES];

    }else if (index == 11){
        collectTableViewController *collectTVC = [[collectTableViewController alloc]init];
        collectTVC.navigationItem.title = @"视频收藏";
        [self.navigationController pushViewController:collectTVC animated:YES];

    }else{
        collectTableViewController *collectTVC = [[collectTableViewController alloc]init];
        collectTVC.navigationItem.title = @"音乐收藏";
        [self.navigationController pushViewController:collectTVC animated:YES];


        //进入收藏页面
       
    }
}


//夜间模式事件
- (void)switch1IsChanged:(UISwitch *)myswitch1
{
    [self.MytableView NightWithType:0];
    [[ThemeManage shareThemeManage]setIsNight:!myswitch1.isOn];



}

//wifi播放视频事件
- (void)switch2IsChanged:(UISwitch *)mySwith2
{

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_MytableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 5) {

        NSString *str = @"本网站(网站地址)刊载的所有内容,包括文字、图片、音频、视频、新闻、以及网页版式设计等均在网上搜集。访问者可将本网站提供的内容或服务用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯本网站及相关权利人的合法权利。除此以外,将本网站任何内容或服务用于其他用途时,须征得本网站及相关权利人的书面许可,并支付报酬";

        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"免费声明" message:str preferredStyle:(UIAlertControllerStyleAlert)];

        UIAlertAction *alerAction = [UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleDefault) handler:nil];
        [alertC addAction:alerAction];
        [self presentViewController:alertC animated:YES completion:nil];


    }else if (indexPath.row == 6){

        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认注销" preferredStyle:(UIAlertControllerStyleAlert)];

        UIAlertAction *alerAction1 = [UIAlertAction actionWithTitle:@"是" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            [AVUser logOut];  //清除缓存用户对象
            AVUser *currentUser = [AVUser currentUser]; // 现在的currentUser是nil了
            self.currentUser = currentUser;
            _strTitle = @"登陆";
            [self.loaginBT setTitle:_strTitle forState:(UIControlStateNormal)];
            self.imgView.image = [UIImage imageNamed:@"comment_profile_default@2x.png"];

        }];
        UIAlertAction * alerAction2 = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];
        [alertC addAction:alerAction1];
        [alertC addAction:alerAction2];
        [self presentViewController:alertC animated:YES completion:nil];


    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
